﻿/*****************************************************
Module Name:  Period Selection Javascript Code
Created By:   Justin Jiang
Created Date: 2008-09-3
Description:
    1. Add Popup function used for Popup a 
fixed window
    2. Add SynScroll function used for Synchro-
nize different DIV scrolling
*****************************************************/
    
// Get control's left position
function getLeft(el)
{   
     return el == null ? 0 : (el.offsetLeft + getLeft(el.offsetParent));   
}

// Get controls's top position
function getTop(el)
{   
     return el == null ? 0 : (el.offsetTop + getTop(el.offsetParent));   
}

// Show or hide div
function ShowDiv(obj)
{

}

// Define PeriodSelection class
function PeriodSelection(){}

// Self defined singleton constructor
PeriodSelection.instance = null;
PeriodSelection.getInstance = function()
{
    if (PeriodSelection.instance == null)
    {
        PeriodSelection.instance = new PeriodSelection();
        PeriodSelection.instance._currentDiv = null;
        PeriodSelection.instance._targetControlId = null;
    }
    return PeriodSelection.instance;
}

// On Show Div
PeriodSelection.prototype.ShowDiv = function(divId, hostControlId)
{
    // Keep only one Div visible
    if (PeriodSelection.getInstance()._currentDiv != null)
    {
        PeriodSelection.getInstance()._currentDiv.style.display = "none";
        if (PeriodSelection.getInstance()._currentDiv.id == divId)
        {
            PeriodSelection.getInstance()._currentDiv = null;
            return;
        }        
    }
    var div = document.getElementById(divId);
    PeriodSelection.getInstance()._currentDiv = div;
    PeriodSelection.getInstance()._targetControlId = hostControlId;
    
    // Override global onmousedown event
    document.body.onmousedown = function()
    {
        if (PeriodSelection.getInstance()._currentDiv == null)
        {
            return;
        }
        // If SourceElement is not current div, then close current div
        var pElement = event.srcElement;
        
        // Ignore target control click
        if (pElement.id == PeriodSelection.getInstance()._targetControlId) 
        {
            return;
        }
        
        // Ignore inner current div control
        var isFind = false;
        while (pElement != null)
        {
            if (pElement.id == PeriodSelection.getInstance()._currentDiv.id)
            {
                isFind = true;
                break;
            }
            pElement = pElement.parentElement;
        }
        
        // If the control is outside current div control
        // hide the div
        if (!isFind)
        {
            PeriodSelection.getInstance()._currentDiv.style.display = "none";
            PeriodSelection.getInstance()._currentDiv = null;
        }
    }
    var hostControl = document.getElementById(hostControlId);
    if (div.style.display != "block")
    {
        div.style.left = getLeft(hostControl) + "px";
        div.style.top = getTop(hostControl) + hostControl.offsetHeight + "px";
        div.style.display = 'block';
    }
}

// On Select
PeriodSelection.prototype.Select = function(chkControlId, valueControlId, hostControlId)
{
    var chkSelection = document.getElementById(chkControlId);
    var txtValue = document.getElementById(valueControlId);
    var hostControl = document.getElementById(hostControlId);
    
    var keys = hostControl.value.split(",");
    var i = 0;
    var isInserted = false;
    if (!chkSelection.checked)
    {
        isInserted = true;
    }
    hostControl.value = "";
    for(i=0; i<keys.length; i++)
    {
        if (keys[i] == "")
        {
            continue;
        }
        if (txtValue.value < keys[i] && !isInserted)
        {
            hostControl.value = hostControl.value + "," + txtValue.value;
            isInserted = true;
        }
        if (txtValue.value != keys[i])
        {
            hostControl.value = hostControl.value + "," + keys[i];
        }
    }
    if (!isInserted)
    {
        hostControl.value = hostControl.value + "," + txtValue.value;
    }
    hostControl.value = hostControl.value.substr(1);
}